﻿/// <reference path="JSintellisense/jquery-1.3.2-vsdoc2.js" />
/************ 高德地图 *********************/


var mapObj = null;
var gzoom = 11;


var resultid = "result1";
var resultbox = "";
var inputid = "tbaddress";
var inputlng = "hidLat";
var inputlat = "hidLng";
var mycity = $("#mycity").val();

function initialize() {

    var _lat = parseFloat($("#hidLat").val());
    var _lng = parseFloat($("#hidLng").val());

    var cityname = $("#hfcityname").val();

    document.getElementById("tbaddress").onkeyup = keydown;

    mapObj = new AMap.Map('', {
        resizeEnable: true,
        zoom: gzoom,
        center: [_lng, _lat]

    });

    resultbox = $("#result1");


}





//键盘按键触发事件
function keydown(event) {

   

    var key = (event || window.event).keyCode;
    var cur = document.getElementById(resultid).curSelect;

    if (key === 40) {//down
        if (cur + 1 < document.getElementById(resultid).childNodes.length) {
            if (document.getElementById(resultid).childNodes[cur]) {
                document.getElementById(resultid).childNodes[cur].style.background = '';
            }
            document.getElementById(resultid).curSelect = cur + 1;
            document.getElementById(resultid).childNodes[cur + 1].style.background = '#CAE1FF';
            $("#" + inputid).val(document.getElementById(resultid).tipArr[cur + 1].name);
        }
    } else if (key === 38) {//up
        if (cur - 1 >= 0) {
            if (document.getElementById(resultid).childNodes[cur]) {
                document.getElementById(resultid).childNodes[cur].style.background = '';
            }
            document.getElementById(resultid).curSelect = cur - 1;

            document.getElementById(resultid).childNodes[cur - 1].style.background = '#CAE1FF';
            $("#" + inputid).val(document.getElementById(resultid).tipArr[cur - 1].name);
        }
    } else if (key === 13) {
        var res = document.getElementById(resultid);
        if (res && res['curSelect'] !== -1) {
            selectResult(document.getElementById(resultid).curSelect);
        }
    } else {
        autoSearch();
    }
}

//自动完成提示
function autoSearch() {
    var keywords = $("#" + inputid).val();

    console.log("keywords=" + keywords);


    var auto;
    AMap.service(["AMap.Autocomplete"], function () {
        var autoOptions = {
            city: mycity //城市，默认全国
        };
        auto = new AMap.Autocomplete(autoOptions);
        if (keywords.length > 0) {
            auto.search(keywords, function (status, result) {
                autocomplete_CallBack(result);
            });
        }
        else {
            resultbox.css({ display: "none" });
        }
    });
}


//输出输入提示结果的回调函数
function autocomplete_CallBack(data) {

    var resultStr = "";
    var tipArr = data.tips;
    if (tipArr && tipArr.length > 0) {
        for (var i = 0; i < tipArr.length; i++) {
            resultStr += "<div id='divid" + resultid + (i + 1) + "' onclick='selectResult(" + i + ")' style=\"font-size: 13px;cursor:pointer;padding:5px 5px 5px 5px;\"" + "data=" + tipArr[i].adcode + ">" + tipArr[i].name + "<span style='color:#C1C1C1;'>" + tipArr[i].district + "</span></div>";
        }
    }
    else {
        resultStr = "";
    }

    document.getElementById(resultid).curSelect = -1;
    document.getElementById(resultid).tipArr = tipArr;
    //document.getElementById(resultid).innerHTML = resultStr;
    resultbox.html(resultStr);


    resultbox.css({ display: "block" });
}


//输出关键字查询结果的回调函数
function placeSearch_CallBack(data) {

    var poiArr = data.poiList.pois;
    var resultCount = poiArr.length;
    /*
     for (var i = 0; i < resultCount; i++) {
     console.log(poiArr[i].name);
     }*/
    if (resultCount > 0) {
        console.log(poiArr[0].location.lng);
        $("#" + inputlng).val(poiArr[0].location.lng);
        $("#" + inputlat).val(poiArr[0].location.lat);
        $("#" + inputid).val(poiArr[0].address + $("#" + inputid).val());
        if ((inputid == "shoppingToAddressArea") || (inputid == "shoppingFromAddressArea")) {
            showShoppingDistance();
        } else if ((inputid == "deliveryFromAddressArea") || (inputid == "deliveryToAddressArea")) {
            showDeliveryDistance();
        }
    }
}



//从输入提示框中选择关键字并查询
function selectResult(index) {
    if (index < 0) {
        return;
    }
    //截取输入提示的关键字部分
    var text = $("#divid" + resultid + (index + 1)).html().replace(/<[^>].*?>.*<\/[^>].*?>/g, "");
    var cityCode = $("#divid" + resultid + (index + 1)).attr('data');

    $("#" + inputid).val(text);
    $("#" + resultid).css({ display: "none" });
    //根据选择的输入提示关键字查询

    mapObj.plugin(["AMap.PlaceSearch"], function () {
        var msearch = new AMap.PlaceSearch();  //构造地点查询类
        AMap.event.addListener(msearch, "complete", placeSearch_CallBack); //查询成功时的回调函数
        msearch.setCity(cityCode);
        msearch.search(text);  //关键字查询查询
    });
}

//定位相关类
$(function () {
    initialize();
})

